package com.example.littleken.mytao;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;

import com.example.littleken.mytao.fragment.FindFragment;
import com.example.littleken.mytao.fragment.MainFragment;
import com.example.littleken.mytao.fragment.MeFragment;

/**
 * 这界面activity
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    protected MainFragment mainFragment = new MainFragment();//首页界面
    protected FindFragment findFragment = new FindFragment();//发现界面
    protected MeFragment meFragment = new MeFragment();//我的界面

    protected LinearLayout MenuMain;
    protected LinearLayout MenuFind;
    protected LinearLayout MenuMe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init_MainMenu();//初始化主页菜单
        //把Fragment添加到MainActivity中（内容RelativeLayout中）
        // 1,获取管理类
        //2,事务添加  默认显示首页Fragment，其他fragment隐藏
        //3,事务提交
        this.getSupportFragmentManager().beginTransaction()
                .add(R.id.container_content, mainFragment)//把Fragment添加到activity_main中的内容布局中,默认显示
                .add(R.id.container_content, findFragment).hide(findFragment)//隐藏
                .add(R.id.container_content, meFragment).hide(meFragment)//隐藏
                .commit();//提交

    }

    /**
     * 初始化主页菜单
     */
    private void init_MainMenu() {
        MenuMain = (LinearLayout) findViewById(R.id.menu_main);
        MenuFind = (LinearLayout) findViewById(R.id.menu_find);
        MenuMe = (LinearLayout) findViewById(R.id.menu_me);

        MenuMain.setOnClickListener(this);//设置监听事件
        MenuFind.setOnClickListener(this);
        MenuMe.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_main://主页
                    this.getSupportFragmentManager()
                            .beginTransaction()
                            .show(mainFragment)
                            .hide(findFragment)
                            .hide(meFragment)
                            .commit();
                break;
            case R.id.menu_find://发现
                this.getSupportFragmentManager()
                        .beginTransaction()
                        .show(findFragment)
                        .hide(mainFragment)
                        .hide(meFragment)
                        .commit();
                break;
            case R.id.menu_me://我的
                this.getSupportFragmentManager()
                        .beginTransaction()
                        .show(meFragment)
                        .hide(mainFragment)
                        .hide(findFragment)
                        .commit();
                break;
        }

    }
}

